package com.geek.factory.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geek.factory.entity.Warehouse;
import com.geek.factory.mapper.WarehouseMapper;
import com.geek.factory.service.IWarehouseService;
import com.geek.factory.utils.RedisUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    private static final String CACHE_NAME = "warehouse";

    @Override
    @Cacheable(value = CACHE_NAME, key = "#id")
    public Warehouse getById(Serializable id) {
        log.info("从数据库查询仓库信息, id: {}", id);
        return super.getById(id);
    }

    @Override
    @CachePut(value = CACHE_NAME, key = "#warehouse.warehouseId")
    public boolean save(Warehouse warehouse) {
        boolean result = super.save(warehouse);
        log.info("新增仓库信息并写入缓存, id: {}, result: {}", warehouse.getWarehouseId(), result);
        return result;
    }

    @Override
    @CachePut(value = CACHE_NAME, key = "#warehouse.warehouseId")
    public boolean updateById(Warehouse warehouse) {
        boolean result = super.updateById(warehouse);
        log.info("更新仓库信息并更新缓存, id: {}, result: {}", warehouse.getWarehouseId(), result);
        return result;
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "#id")
    public boolean removeById(Serializable id) {
        boolean result = super.removeById(id);
        log.info("删除仓库信息并删除缓存, id: {}, result: {}", id, result);
        return result;
    }
}